package com.ipmirror.component;

import com.icesoft.faces.component.datapaginator.DataPaginator;
import com.icesoft.faces.component.ext.RowSelectorEvent;
import com.ipmirror.component.interfaces.IPopupSelectCustomer;
import com.ipmirror.rebuild.interfaces.ICustomerBean;
import com.ipmirror.rebuild.view.PopupCustomerView;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import java.util.List;

/**
 * Created by denny
 * User: denny
 * Date: Jan 13, 2011
 * Time: 10:19:26 AM
 */
public class PopupSelectCustomer implements IPopupSelectCustomer{
    private static Logger log = Logger.getLogger(PopupSelectCustomer.class);
    private List<PopupCustomerView> customerList;
    private String inputName;
    private boolean render;
    private String title;
    private Integer selectedCustomerPk;
    private String selectedCustomerName;
    private boolean rowSelected;
    private boolean renderPaginator;

    @EJB(name = "IPMEjb/CustomerBean/local")
    private ICustomerBean customerService;
    private boolean ascending;
    private String sortedColumn = "loginName";
    private DataPaginator paginator;
    private boolean disableOkButton;
    private PopupCustomerView selectedCustomer;

    @PostConstruct
    public void init() {
        selectedCustomer=null;
    }

    public List<PopupCustomerView> getCustomerList() {
        return customerList;
    }

    public void setCustomerList(List<PopupCustomerView> customerList) {
        this.customerList = customerList;
    }

    public String getInputName() {
        return inputName;
    }

    public void setInputName(String inputName) {
        this.inputName = inputName;
    }

    public boolean isRender() {
        return render;
    }

    public void setRender(boolean render) {
        if(render && paginator!=null){
            paginator.gotoFirstPage();
        }
        if(customerList!=null){
            for(PopupCustomerView view : customerList){
                view.setRowSelected(false);
            }
        }
        selectedCustomer=null;
        this.render = render;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getSelectedCustomerPk() {
        return selectedCustomerPk;
    }

    public void setSelectedCustomerPk(Integer selectedCustomerPk) {
        this.selectedCustomerPk = selectedCustomerPk;
    }

    public String getSelectedCustomerName() {
        return selectedCustomerName;
    }

    public void setSelectedCustomerName(String selectedCustomerName) {
        this.selectedCustomerName = selectedCustomerName;
    }

    public void inputNameValueChangeListiener(ValueChangeEvent valueChangeEvent) {
        inputName = (String) valueChangeEvent.getNewValue();

    }

    private void initCustomerList(String newValue) {
        if (customerList != null) {
            customerList.clear();
        }
        if (StringUtils.isNotBlank(newValue)) {
            customerList = customerService.findCustomerByLoginName(newValue);
            if(customerList.size()==0){
                selectedCustomer=null;
            }
            paginator.gotoFirstPage();
        }
    }

    public void rowSelectedListiener(RowSelectorEvent rowSelectorEvent) {
        int selectedIndex = rowSelectorEvent.getRow();
        selectedCustomer = customerList.get(selectedIndex);
        for (PopupCustomerView view : customerList) {
            if (!view.equals(selectedCustomer)) {
                view.setRowSelected(false);
            }
        }
    }

    public void search(ActionEvent actionEvent) {
        initCustomerList(inputName);
    }

    public void setAscending(boolean ascending) {
        this.ascending = ascending;
    }

    public boolean getAscending() {
        return ascending;
    }

    public void setSortedColumn(String sortedColumn) {
        this.sortedColumn = sortedColumn;
    }

    public String getSortedColumn() {
        return sortedColumn;
    }

    public boolean isRowSelected() {
        return rowSelected;
    }

    public void setRowSelected(boolean rowSelected) {
        this.rowSelected = rowSelected;
    }

    public void closeOrCancelPopup() {
        render = false;
    }

    public DataPaginator getPaginator() {
        return paginator;
    }

    public void setPaginator(DataPaginator paginator) {
        this.paginator = paginator;
    }

    public boolean isRenderPaginator() {
        if(paginator==null || paginator.getPageCount()<=1){
            return false;
        }else{
            return true;
        }
    }

    public void setRenderPaginator(boolean renderPaginator) {
        this.renderPaginator = renderPaginator;
    }

    public void selectOk(){
        render=false;
    }

    public boolean isDisableOkButton() {
        if (customerList != null && customerList.size() > 0) {
            return false;
        } else {
            return true;
        }
    }

    public void setDisableOkButton(boolean disableOkButton) {
        this.disableOkButton = disableOkButton;
    }

    public PopupCustomerView getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(PopupCustomerView selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }
}
